/* 
 * Copyright 2013 Sébastien Mouquet.
 * 
 * This file is part of ECovoiturage.
 * 
 * ECovoiturage is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ECovoiturage is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Foobar. If not, see http://www.gnu.org/licenses/.
 */
package com.smouquet.ecovoiturage.ui.page.root;

import com.google.gwt.thirdparty.guava.common.eventbus.EventBus;
import com.google.gwt.thirdparty.guava.common.eventbus.Subscribe;
import com.smouquet.ecovoiturage.core.event.TrajetMidiSelectedEvent;
import com.smouquet.ecovoiturage.ui.NotificationUtil;
import com.smouquet.ecovoiturage.ui.page.AbstractUI;
import com.smouquet.ecovoiturage.ui.page.LoginView;
import com.vaadin.navigator.Navigator;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Runo;
import org.vaadin.addon.borderlayout.BorderLayout;

/**
 * <code>UI</code> correspondant à la partie "ECovoiturage" de l'application.
 * 
 * @author sebastien.mouquet
 */
public class RootUI extends AbstractUI implements Button.ClickListener
{
    private Button btnTrajetMidi;
    private Button btnCompte;
    private Button btnAPropos;
    private Button btnDeconnexion;
    private HorizontalLayout layoutLinkSecondary;
    private Button btnListeTrajets;
    private Button btnAjoutTrajet;
    private Button btnModifierTrajet;
    private Panel content;
    
    private Navigator rootNavigator;
    private EventBus eventBus;
    
    private Integer currentTrajetMidiId;

    @Override
    protected void initialize()
    {
        //
        // Layout
        //
        BorderLayout layout = new BorderLayout();
        layout.setMargin(true);
        layout.setSizeFull();
        setContent(layout);   
        
        //
        // Top
        //
        HorizontalLayout layoutTop = new HorizontalLayout();
        layoutTop.setWidth(100, Unit.PERCENTAGE);
        layoutTop.setHeight(100, Unit.PIXELS);
        layout.addComponent(layoutTop, BorderLayout.Constraint.NORTH);
        
        //
        // Top - Title
        //
        VerticalLayout layoutTitle = new VerticalLayout();
        layoutTitle.setSizeFull();
        layoutTop.addComponent(layoutTitle);
        layoutTop.setExpandRatio(layoutTitle, 1);
        
        Label lbTitle = new Label("ECovoiturage");
        lbTitle.setSizeUndefined();
        lbTitle.addStyleName(Runo.LABEL_H1);
        layoutTitle.addComponent(lbTitle);
        layoutTitle.setComponentAlignment(lbTitle, Alignment.BOTTOM_CENTER);
        
        Label lbSubtitle = new Label("Covoiturage en entreprise");
        lbSubtitle.setSizeUndefined();
        layoutTitle.addComponent(lbSubtitle);
        layoutTitle.setComponentAlignment(lbSubtitle, Alignment.TOP_CENTER);

        //
        // Top - Link
        //
        VerticalLayout layoutLink = new VerticalLayout();
        layoutLink.setSpacing(true);
        layoutLink.setSizeFull();
        layoutTop.addComponent(layoutLink);
        layoutTop.setExpandRatio(layoutLink, 2);
        
        //
        // Top - Link - Primary
        //
        HorizontalLayout layoutLinkPrimary = new HorizontalLayout();
        layoutLinkPrimary.setSpacing(true);
        layoutLink.addComponent(layoutLinkPrimary);
        layoutLink.setComponentAlignment(layoutLinkPrimary, Alignment.BOTTOM_CENTER);
        
        btnTrajetMidi = new Button("Trajet du Midi");
        btnTrajetMidi.addStyleName(Runo.BUTTON_DEFAULT);
        btnTrajetMidi.addClickListener(this);
        layoutLinkPrimary.addComponent(btnTrajetMidi);
        
        btnCompte = new Button("Mon Compte");
        btnCompte.addStyleName(Runo.BUTTON_DEFAULT);
        btnCompte.addClickListener(this);
        layoutLinkPrimary.addComponent(btnCompte);
        
        btnAPropos = new Button("A Propos");
        btnAPropos.addStyleName(Runo.BUTTON_DEFAULT);
        btnAPropos.addClickListener(this);
        layoutLinkPrimary.addComponent(btnAPropos);
        
        btnDeconnexion = new Button("Deconnexion");
        btnDeconnexion.addStyleName(Runo.BUTTON_DEFAULT);
        btnDeconnexion.addClickListener(this);
        layoutLinkPrimary.addComponent(btnDeconnexion);
        
        //
        // Top - Link - Secondary
        //
        layoutLinkSecondary = new HorizontalLayout();
        layoutLinkSecondary.setSpacing(true);
        layoutLinkSecondary.setImmediate(true);
        layoutLink.addComponent(layoutLinkSecondary);
        layoutLink.setComponentAlignment(layoutLinkSecondary, Alignment.TOP_CENTER);
        
        btnListeTrajets = new Button("Liste des Trajets");
        btnListeTrajets.addStyleName(Runo.BUTTON_SMALL);
        btnListeTrajets.addClickListener(this);
        
        btnAjoutTrajet = new Button("Ajouter un Trajet");
        btnAjoutTrajet.addStyleName(Runo.BUTTON_SMALL);
        btnAjoutTrajet.addClickListener(this);
        
        btnModifierTrajet = new Button("Voir/Modifier le Trajet");
        btnModifierTrajet.addStyleName(Runo.BUTTON_SMALL);
        btnModifierTrajet.addClickListener(this);
        
        // Top - Blank
        VerticalLayout layoutBlank = new VerticalLayout();
        layoutBlank.setSizeFull();
        layoutTop.addComponent(layoutBlank);
        layoutTop.setExpandRatio(layoutBlank, 1);
        
        //
        // Content
        //
        content = new Panel("");
        content.setSizeFull();
        layout.addComponent(content, BorderLayout.Constraint.CENTER);
        
        //
        // Navigator
        //
        rootNavigator = new Navigator(this, content);
        rootNavigator.addView(TrajetMidiListView.VIEW_NAME, TrajetMidiListView.class);
        rootNavigator.addView(TrajetMidiAddView.VIEW_NAME, TrajetMidiAddView.class);
        rootNavigator.addView(TrajetMidiEditView.VIEW_NAME, TrajetMidiEditView.class);
        
        //
        // EventBus
        //
        eventBus = new EventBus();
        eventBus.register(this);
        
        
        doNavigateToTrajetMidiList();
    }
    

    @Override
    public void buttonClick(ClickEvent event)
    {
        if(btnTrajetMidi.equals(event.getButton()) ||
           btnListeTrajets.equals(event.getButton()))
        {
            doNavigateToTrajetMidiList();
        }
        else if(btnAjoutTrajet.equals(event.getButton()))
        {
            doNavigateToTrajetMidiAdd();
        }
        else if(btnModifierTrajet.equals(event.getButton()))
        {
            doNavigateToTrajetMidiEdit();
        }
        else if(btnDeconnexion.equals(event.getButton()))
        {
            doDeconnexion();
        }
        else
        {
            NotificationUtil.showInformationNotification("Fonctionnalité non implémentée pour le moment...");
        }
    }
    
    @Subscribe
    public void onTrajetMidiSelected(TrajetMidiSelectedEvent event)
    {
        btnModifierTrajet.setEnabled(true);
        currentTrajetMidiId = event.getItem().getIdTrajet();
    }
    
    
    private void doNavigateToTrajetMidiList()
    {
        content.setCaption(btnTrajetMidi.getCaption()+" > "+btnListeTrajets.getCaption());
            
        layoutLinkSecondary.addComponent(btnListeTrajets);
        layoutLinkSecondary.addComponent(btnAjoutTrajet);
        layoutLinkSecondary.addComponent(btnModifierTrajet);
        
        btnModifierTrajet.setEnabled(false);
        
        currentTrajetMidiId = null;

        rootNavigator.navigateTo(TrajetMidiListView.VIEW_NAME);
    }
    
    private void doNavigateToTrajetMidiAdd()
    {
        content.setCaption(btnTrajetMidi.getCaption()+" > "+btnAjoutTrajet.getCaption());

        rootNavigator.navigateTo(TrajetMidiAddView.VIEW_NAME);
    }
    
    public void doNavigateToTrajetMidiEdit()
    {
        content.setCaption(btnTrajetMidi.getCaption()+" > "+btnModifierTrajet.getCaption());

        rootNavigator.navigateTo(TrajetMidiEditView.VIEW_NAME);
    }
    
    private void doDeconnexion()
    {
        getMainNavigator().navigateTo(LoginView.VIEW_NAME);
    }

    
    /**
     * @return Le <code>Navigator</code> pour la partie "ECovoiturage"
     *         de l'application.
     */
    public Navigator getRootNavigator() {
        return rootNavigator;
    }

    /**
     * @return Le bus d'évènements utilisé pour la partie "ECovoiturage"
     *         de l'application.
     */
    public EventBus getEventBus() {
        return eventBus;
    }

    /**
     * @return L'identifiant correspondant au trajet du midi sélectionné via
     *         la vue {@link TrajetMidiListView}.
     */
    public Integer getCurrentTrajetMidiId() {
        return currentTrajetMidiId;
    }

    public void setCurrentTrajetMidiId(Integer currentTrajetMidiId) {
        this.currentTrajetMidiId = currentTrajetMidiId;
    }
}
